package com.tablefood.service.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.tablefood.dao.CategoryDao;
import com.tablefood.domain.ShopCategory;
import com.tablefood.service.CategoryService;
@Transactional
public class CategoryServiceImpl implements CategoryService {

	@Resource
	private CategoryDao categoryDao;
	
	@Override
	public List<ShopCategory> getAllCategory() {
	 
		List<ShopCategory> categoryList = categoryDao.getAllCategory();
		categoryList = getTree(categoryList, (long) 0);
		return categoryList;
	}
	
	//数据处理，按照顺序排序
	public List<ShopCategory> getTree(List<ShopCategory> cates, Long pid){	
		List<ShopCategory> tree = new ArrayList();
		for(ShopCategory cate : cates){	
			if(cate.getParentid() == pid){
				tree.add(cate);
				tree.addAll(getTree(cates, cate.getCateid()));
			}
		}
	    return tree;
	}


	@Override
	public boolean delCategory(Long cateid) {
	 
		
		// 查看有没有孩子
		if(categoryDao.hasChild(cateid)){
			return false;
		}else{
			return categoryDao.delCategory(cateid);
		}
	}

	@Override
	public ShopCategory getCategoryByCateId(Long cateid) {
	 
		return categoryDao.getCategoryByCateId(cateid);
	}

	@Override
	public boolean addCategory(ShopCategory category) {
	 
		return categoryDao.addCategory(category);
	}

	@Override
	public boolean checkCategoryTitle(String title) {
	 
		return categoryDao.checkCategoryTitle(title);
	}

	@Override
	public boolean updateCategory(ShopCategory category) {
	 
		return categoryDao.updateCategory(category);
	}

	@Override
	public boolean hasParent(Long cateid) {
		return categoryDao.hasParent(cateid);
	}

	@Override
	public boolean hasChild(Long cateid) {
	 
		return categoryDao.hasChild(cateid);
	}

}
